package malvados.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import malvados.app.bin.AdapterListView;
import malvados.app.bin.Favorito;
import malvados.app.bin.ItemListView;
import malvados.app.bin.ListaFavoritos;


public class ViewFavoritos extends Activity {
	
	private ListView listaFavoritos;
	private ArrayList<ItemListView> itens;
	private AdapterListView adapterListView;


	public ViewFavoritos() {}
	
	public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favoritos);
        
        listaFavoritos = (ListView)findViewById(R.id.listFavoritos);
        listaFavoritos.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ItemListView item = adapterListView.getItem(arg2);
				if(item.getIdTirinha() != 0){
					Intent resultIntent = new Intent();
					resultIntent.putExtra("IdTirinha", String.valueOf(item.getIdTirinha()));
					setResult(1, resultIntent);
					finish();
				}
			}
        	
		});
        carregaLista();
    }
	
	private void carregaLista(){
		itens = new ArrayList<ItemListView>();
		List<Favorito> lista = ListaFavoritos.carregaListaFavoritos();
		if(lista.size() > 0){
			Iterator<Favorito> iterator = lista.iterator();
			while(iterator.hasNext()){
				Favorito temp = iterator.next();
				ItemListView itemtemp = new ItemListView(temp.getDescricao(),R.drawable.favorito);
				itemtemp.setIdTirinha(Integer.valueOf(temp.getCodigoTirinha()));
				itens.add(itemtemp);
			}
		}else{
			ItemListView itemtemp = new ItemListView("Nenhuma Tirinha em Favoritos", R.drawable.favorito);
			itemtemp.setIdTirinha(0);
			itens.add(itemtemp);
		}
		
		
		adapterListView = new AdapterListView(this, itens);
		listaFavoritos.setAdapter(adapterListView);
		listaFavoritos.setCacheColorHint(Color.TRANSPARENT);

	}


}
